package com.bawei.lookfor2204a.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

/**
 * 作者：赵亮
 * 时间：2024/10/15 12:07
 * 功能：基类Activity
 */
abstract class BaseActivity<VM : ViewModel, VDB : ViewDataBinding>(private val layoutId: Int) :
    AppCompatActivity() {

    lateinit var binding: VDB       // 处理页面控件- 代替findViewById
    lateinit var viewModel: VM      // 调用网络，给页面提供数据

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, layoutId)    // 实例化binding
        viewModel = ViewModelProvider(this).get(getViewModel())     // 实例化ViewModel

        initView()  // 初始化View

        initData()  // 初始化数据
    }

    abstract fun initData()

    abstract fun initView()

    abstract fun getViewModel(): Class<VM>

    override fun onDestroy() {
        super.onDestroy()
        // 避免内存泄露
        binding.unbind()
    }
}